//
//  IBGiftShowView.m
//  烟花特效
//
//  Created by apple on 15/7/10.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "IBGiftShowView.h"
#import "IBMagicGiftView.h"
#import "IBGiftBox.h"
#import "IBFireWorksView.h"

@interface IBGiftShowView ()

@property (nonatomic,strong) IBMagicGiftView * giftView; //要显示的礼物
@property (nonatomic,strong) IBGiftBox * giftBox; //礼物盒
@property (nonatomic,strong) IBFireWorksView * fireWorksView; //烟花背景
@end

@implementation IBGiftShowView

-(instancetype)initWithFrame:(CGRect)frame GiftImageUrl:(NSString *)url
{
    self = [super initWithFrame:frame];
    if (self) {
        WS(ws);
        [[SDWebImageManager sharedManager]downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageLowPriority progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            [ws initWithFrame:frame GiftImage:image];
        }];
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame GiftImage:(UIImage *)img
{
    self = [super initWithFrame:frame];
    if (self) {
        _giftView = [[IBMagicGiftView alloc]initWithImage:img Frame:CGRectZero StarTime:1.5];
        _giftBox = [[IBGiftBox alloc]initWithGiftView:_giftView frame:CGRectMake(100, 100, 200, 200)];
        _giftBox.center = self.center;
        [self addSubview:_giftBox];
        [self startAnimation];
    }
    return self;
}

//播放礼盒效果
-(void)startAnimation
{
    __weak typeof(self) weakSelf = self;
    //动画播放完后再播放星星点缀
    [_giftBox startAnimationTime:3 Completed:^{
        [weakSelf boxAnimationFinished];
    }];
}

//礼盒播放完之后
-(void)boxAnimationFinished
{
    [_giftView startMagicStar];
    //生成烟花背景
    _fireWorksView = [[IBFireWorksView alloc]initWithFrame:self.frame];
    [self addSubview:_fireWorksView];
    [self sendSubviewToBack:_fireWorksView];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
